<!DOCTYPE html>
<html lang="en" class="bg-white">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>QQ扫码验证</title>
    <link rel="stylesheet" href="/Assets/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="/Assets/module/admin.css?v=318"/>
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
<div class="text-center" style="padding: 25px 25px 15px 25px;">
    <div class="layui-text">
        <div style="margin-top:1rem;margin-bottom:1rem;color:#666" id="login">
            <span id="loginmsg">请使用QQ手机版扫描二维码</span><span id="loginload" style="color: #790909;">.</span>
            <div id="qrimg" style="height: 100px"></div>
        </div>
    </div>

</div>

<!-- js部分 -->
<script type="text/javascript" src="/Assets/libs/layui/layui.js"></script>
<script type="text/javascript" src="/Assets/js/common.js?v=318"></script>
<script>
    layui.use(['layer', 'admin', 'form', 'notice', 'jquery'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var notice = layui.notice;

        var interval1,interval2;
        function delCookie(name)
        {
            var exp = new Date();
            exp.setTime(exp.getTime() - 1);
            var cval=getCookie(name);
            if(cval!=null)
                document.cookie= name + "="+cval+";expires="+exp.toGMTString();
        }
        function setCookie(name,value)
        {
            var exp = new Date();
            exp.setTime(exp.getTime() + 30*1000);
            document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
        }
        function getCookie(name)
        {
            var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
            if(arr=document.cookie.match(reg))
                return unescape(arr[2]);
            else
                return null;
        }
        function getqrpic(force){
            force = force || false;
            var qrsig = getCookie('qrsig');
            var qrimg = getCookie('qrimg');
            if(qrsig!=null && qrimg!=null && force==false){
                $('#qrimg').attr('qrsig',qrsig);
                $('#qrimg').html('<img id="qrcodeimg" onclick="getqrpic(true)" src="data:image/png;base64,'+qrimg+'" title="点击刷新">');
                if( /Android|SymbianOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Windows Phone|Midp/i.test(navigator.userAgent) && navigator.userAgent.indexOf("QQ/") == -1) {
                    $('#mobile').show();
                }
            }else{
                $.ajax({
                    type: "POST",
                    url: '/api.php/Qrlogin/getqrpic',
                    data: {'r':Math.random(1)},
                    dataType: "json",
                    success: function(data) {
                        if(data.code == 0){
                            setCookie('qrsig',data.data.qrsig);
                            setCookie('qrimg',data.data.data);
                            $('#qrimg').attr('qrsig',data.data.qrsig);
                            $('#qrimg').html('<img id="qrcodeimg" onclick="getqrpic(true)" src="data:image/png;base64,'+data.data.data+'" title="点击刷新">');
                            if( /Android|SymbianOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Windows Phone|Midp/i.test(navigator.userAgent) && navigator.userAgent.indexOf("QQ/") == -1) {
                                $('#mobile').show();
                            }
                        }else{
                            $('#loginmsg').html(data.data.msg);
                        }
                    },
                    error: function() {
                        $('#loginmsg').html("服务器错误！");
                    }
                });
            }
        }

        var layerData = JSON.parse(JSON.stringify(admin.getLayerData()));

        function loadScript(c) {
            if ($('#login').attr("data-lock") === "true") return;
            var qrsig=$('#qrimg').attr('qrsig');
            layerData.qrsig = decodeURIComponent(qrsig);
            $.ajax({
                type: "POST",
                url: '/api.php/Qrlogin/qqLogin',
                data: layerData,
                dataType: "json",
                success: function(data) {
                    var msg='请扫描二维码';

                    switch(data.data.code) {
                        case 0:
                            $('#login').hide();
                            $('#login').attr("data-lock", "true");
                            switch (layerData.type){
                                case 'replaceAuth':
                                    var i = parent.layui.layer.confirm('确定要将<span class="layui-badge layui-badge-orange">'+layerData.auth_info+'</span>更换成<span class="layui-badge layui-badge-green">'+layerData.replace_info+'</span>吗？', {
                                        skin: 'layui-layer-admin',
                                        shade: .1
                                    }, function (i) {
                                        parent.layui.layer.close(i);
                                        notice.msg('正在执行中..', {icon: 4, close: true});
                                        $.ajax({
                                            type: "POST",
                                            url: '/api.php/Qrlogin/replaceAuth',
                                            data: layerData,
                                            dataType: "json",
                                            success: function(data) {
                                                notice.destroy();
                                                if (data.code == 0) {
                                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                    parent.layer.close(index);
                                                    parent.layui.layer.alert('更换授权成功！',{icon:1,skin: 'layui-layer-admin',closeBtn:0});
                                                } else {
                                                    if(data.data.replace == 1){
                                                        var i = parent.layui.layer.alert(data.msg,{icon:2,closeBtn:0},function(){
                                                            parent.layui.layer.close(i);
                                                            var i2 = parent.layer.prompt({
                                                                title:'修改更换的授权',
                                                                value: layerData.replace_info,
                                                                skin: 'layui-layer-admin',
                                                                btn2:function(index){
                                                                    //点击取消执行的代码
                                                                }
                                                            },function(value,index){
                                                                layerData.replace_info = value;
                                                                parent.layui.layer.close(i2);
                                                                parent.layui.layer.alert('修改成功，请再次点击更换授权按钮！',{icon:1,skin: 'layui-layer-admin',closeBtn:0});
                                                            })
                                                        });
                                                    }else{
                                                        parent.layui.layer.alert(data.msg,{icon:2,skin: 'layui-layer-admin',closeBtn:0});
                                                    }
                                                }
                                            },
                                            error: function() {
                                                notice.destroy();
                                                notice.error({
                                                    title: '更换通知',
                                                    message: '服务器错误',
                                                    animateInside:true,
                                                    position:'topCenter',
                                                    transitionOut:'flipOutX',
                                                    transitionOutMobile: 'flipOutX',
                                                    displayMode:'2',
                                                    audio:'1'
                                                });
                                            }
                                        });
                                    });
                                    break;
                                case 'userLogin':
                                    notice.msg('正在执行中..', {icon: 4, close: true});
                                    $.ajax({
                                        type: "POST",
                                        url: '/api.php/Qrlogin/userLogin',
                                        data: layerData,
                                        dataType: "json",
                                        success: function(data) {
                                            notice.destroy();
                                            if (data.code == 0) {
                                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                parent.layer.close(index);
                                                if(!data.data){
                                                    parent.layui.layer.alert('登录成功！',{icon:1,skin: 'layui-layer-admin',closeBtn:0});
                                                }else{
                                                    var content = '<div class="layui-card" style="margin: 30px 30px 30px 30px;border-radius: 10px"><div class="layui-card-body">';
                                                    $.each(data.data, function(index, value){
                                                        content += '<a onclick="qrcodeLogin(\''+value.username+'\',\''+value.appid+'\')"><div style="'+(index==0?'':'border-top:1px solid #e6e6e6;')+'border-radius: 4px;height: 60px;line-height:60px;position: relative">' +
                                                            '<img style="margin-left:10px;width: 40px;height: 40px;border-radius: 50%;border: 3px solid #fff;" src="//q1.qlogo.cn/g?b=qq&nk=2129876388&s=100">' +
                                                            '<div style="font-size:16px;color:#030718;position: absolute;top:-10px;left:60px">'+value.username+'</div>' +
                                                            '<div style="font-size:10px;color:#878b97;position: absolute;top:10px;left:60px">所属应用：'+value.appname+'</div>' +
                                                            '</div></a>';
                                                    });
                                                    content += '</div></div>';
                                                    parent.layui.admin.open({
                                                        type: 1,
                                                        title: '选择要登录的账号',
                                                        content: content
                                                    });
                                                }
                                            } else {
                                                getqrpic();
                                                notice.error({
                                                    title: '登录通知',
                                                    message: data.msg,
                                                    animateInside:true,
                                                    position:'topCenter',
                                                    transitionOut:'flipOutX',
                                                    transitionOutMobile: 'flipOutX',
                                                    displayMode:'2',
                                                    audio:'1'
                                                });
                                            }
                                        },
                                        error: function() {
                                            notice.destroy();
                                            getqrpic();
                                            notice.error({
                                                title: '登录通知',
                                                message: '服务器错误',
                                                animateInside:true,
                                                position:'topCenter',
                                                transitionOut:'flipOutX',
                                                transitionOutMobile: 'flipOutX',
                                                displayMode:'2',
                                                audio:'1'
                                            });
                                        }
                                    });
                                    break;
                                case 'binding':
                                    parent.layui.notice.msg('正在执行中..', {icon: 4, close: true});
                                    $.ajax({
                                        type: "POST",
                                        url: '/api.php/Qrlogin/binding',
                                        data: layerData,
                                        dataType: "json",
                                        success: function(data) {
                                            parent.layui.notice.destroy();
                                            if (data.code == 0) {
                                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                                parent.layer.close(index);
                                                var content = '<div class="layui-card"><div class="layui-card-body"><form class="layui-form"><div class="layui-form-item">\n' +
                                                    '            <label class="layui-form-label layui-form-required">数据列表:</label>\n' +
                                                    '            <div class="layui-input-block"><input type="checkbox" name="type" value="checkall" lay-skin="primary" lay-filter="allChoose" title="全选">';
                                                $.each(data.data, function(index, value){
                                                    content += '<input type="checkbox" name="list[]" value="'+value.id+'" lay-skin="primary" title="'+value.auth_info+'">';
                                                });
                                                content += '</div></div><div class="layui-form-item text-right">\n' +
                                                    '            <button class="layui-btn layui-btn-primary" type="button" lay-filter="selectAll" lay-submit>全选</button>\n' +
                                                    '            <button type="button" class="layui-btn" lay-filter="BindingSubmit" lay-submit>绑定</button>\n' +
                                                    '        </div></form></div></div>';
                                                parent.layui.admin.open({
                                                    type: 1,
                                                    title: '选择要绑定的数据',
                                                    content: content,
                                                    success: function (layero, dIndex) {
                                                        parent.layui.form.render();
                                                        parent.layui.jquery(layero).children('.layui-layer-content').css('overflow', 'visible');
                                                    }
                                                });
                                            } else {
                                                cleartime();
                                                $('#login').show();
                                                $('#login').attr("data-lock", "false");
                                                getqrpic();
                                                parent.layer.alert(data.msg, {title:'绑定通知',icon:3,skin: 'layui-layer-admin',shade: .1,closeBtn:0});
                                            }
                                        },
                                        error: function() {
                                            parent.layui.notice.destroy();
                                            cleartime();
                                            $('#login').show();
                                            $('#login').attr("data-lock", "false");
                                            getqrpic();
                                            parent.layer.alert('服务器错误', {title:'绑定通知',icon:2,skin: 'layui-layer-admin',shade: .1,closeBtn:0});
                                        }
                                    });
                                    break;
                                case 'download':
                                    parent.layui.notice.msg('正在执行中..', {icon: 4, close: true});
                                    $.ajax({
                                        type: "POST",
                                        url: '/api.php/Qrlogin/download',
                                        data: layerData,
                                        dataType: "json",
                                        success: function(data) {
                                            parent.layui.notice.destroy();
                                            if (data.code == 0) {
                                                parent.layui.layer.closeAll();
                                                parent.layui.notice.success({
                                                    title: '获取通知',
                                                    message: data.msg,
                                                    animateInside:true,
                                                    position:'topCenter',
                                                    transitionOut:'flipOutX',
                                                    transitionOutMobile: 'flipOutX',
                                                    displayMode:'2',
                                                    audio:'1'
                                                });
                                                window.open(data.data.url);
                                            } else {
                                                cleartime();
                                                $('#login').show();
                                                $('#login').attr("data-lock", "false");
                                                getqrpic();
                                                parent.layui.notice.error({
                                                    title: '获取通知',
                                                    message: data.msg,
                                                    animateInside:true,
                                                    position:'topCenter',
                                                    transitionOut:'flipOutX',
                                                    transitionOutMobile: 'flipOutX',
                                                    displayMode:'2',
                                                    audio:'1'
                                                });
                                            }
                                        },
                                        error: function() {
                                            parent.layui.notice.destroy();
                                            cleartime();
                                            $('#login').show();
                                            $('#login').attr("data-lock", "false");
                                            getqrpic();
                                            parent.layui.notice.error({
                                                title: '获取通知',
                                                message: '服务器错误',
                                                animateInside:true,
                                                position:'topCenter',
                                                transitionOut:'flipOutX',
                                                transitionOutMobile: 'flipOutX',
                                                displayMode:'2',
                                                audio:'1'
                                            });
                                        }
                                    });
                                    break;
                            }
                            break;
                        case 1:
                            getqrpic();
                            msg = '请重新扫描二维码';
                            break;
                        case 2:
                            msg = '使用QQ手机版扫描二维码';
                            break;
                        case 3:
                            msg = '扫码成功，请在手机上确认授权登录';
                            break;
                        case 4:
                            cleartime();
                            getqrpic();
                            msg = 'QQ' + data.data.qq + '未通过验证，请使用购买授权的QQ扫码！';
                            break;
                        case 5:
                            cleartime();
                            getqrpic();
                            msg = 'QQ验证失败，请解除登录异常后重试！';
                            break;
                        case 6:
                            cleartime();
                            getqrpic();
                            msg = data.msg;
                            break;
                        default:
                            msg = data.msg;
                            break;
                    }
                    $('#loginmsg').html(msg);
                },
                error: function() {
                    $('#loginmsg').html("服务器错误！");
                }
            });
        }
        function loginload(){
            if ($('#login').attr("data-lock") === "true") return;
            var load=document.getElementById('loginload').innerHTML;
            var len=load.length;
            if(len>2){
                load='.';
            }else{
                load+='.';
            }
            document.getElementById('loginload').innerHTML=load;
        }
        function cleartime(){
            // clearInterval(interval1);
            // clearInterval(interval2);
            delCookie('qrsig');
            delCookie('qrimg');
        }
        form.on('submit(replace)', function (data) {

            return false;
        });
        $(document).ready(function(){
            getqrpic();
            interval1=setInterval(loginload,1000);
            interval2=setInterval(loadScript,1500);
        });

    });
</script>
</body>
</html>